import torch
import torch.nn as nn

class MyModel(nn.Module):
    def __init__(self, input_channels, input_size, hidden_size, num_layers, time_steps, LSTM_out_size):
        super(MyModel, self).__init__()
        
        # LSTM layer
        self.lstm = nn.LSTM(input_size=input_size, hidden_size=hidden_size, num_layers=num_layers, batch_first=True)

        # Convolutional layers
        self.conv1 = nn.Conv1d(in_channels=input_channels, out_channels=64, kernel_size=8)
        self.conv2 = nn.Conv1d(in_channels=64, out_channels=32, kernel_size=16)
        self.conv3 = nn.Conv1d(in_channels=32, out_channels=16, kernel_size=3)
        self.relu = nn.ReLU()
        self.sigmoid = nn.Sigmoid()
        self.pool = nn.MaxPool1d(kernel_size=2)
        self.dropout = nn.Dropout(0.5)
                
        # Fully connected layer
        self.flatten = nn.Flatten()
        self.fc1 = nn.Linear(64 + LSTM_out_size, 10)
        self.fc2 = nn.Linear(10, 1)
        self.fc3 = nn.Linear(hidden_size * time_steps, LSTM_out_size)
        
    def forward(self, x1, x2):
        # x1 shape: (batch_size=256, time_steps, features_num=82)
        # x2 shape: (batch_size=256, features_num=82)
        # LSTM layer
        x1, _ = self.lstm(x1)  # (batch_size, time_steps, hidden_size)
        x1 = self.dropout(x1)

        # CNN layers
        x2 = x2.unsqueeze(dim=1)            # torch.Size([256, 1, 82])
        x2 = self.relu(self.conv1(x2))      # torch.Size([256, 64, 75])
        x2 = self.pool(x2)                  # torch.Size([256, 64, 37])
        x2 = self.relu(self.conv2(x2))      # torch.Size([256, 32, 22])
        x2 = self.pool(x2)                  # torch.Size([256, 32, 11])
        x2 = self.relu(self.conv3(x2))      # torch.Size([256, 16, 9])
        x2 = self.pool(x2)                  # torch.Size([256, 16, 4])
        x2 = self.dropout(x2)

        # full-connected layer
        x1 = self.flatten(x1)               # [batch_size, time_steps * hidden_size]
        x1 = self.relu(self.fc3(x1))        # [batch_size, LSTM_out_size=2]
        x2 = self.flatten(x2)               # [batch_size, 16 * 4]
        x = torch.concat((x2, x1), dim=1)   # [batch_size, 64 + LSTM_out_size]
        x = self.relu(self.fc1(x))          # torch.Size([256, 10])
        x = self.sigmoid(self.fc2(x))       # torch.Size([256, 1])
        
        return x